
const instances:Service[] = [];
const serviceInter = Symbol();
const onlyOne = Symbol();

/**
 * 单例模式 实现服务的单一职责
 */
export abstract class Service {

    serviceName = this.constructor.name;
    static isInstance = false;


    static  get hasInstance(){
        return !!instances[this.name];
    }

    public static  get instance(){
        return this.isInstance ? this[onlyOne] : Service.createInstance(this)
    }

    static  createInstance(ServiceClass:any){
        if(Service.hasInstance){
            console.log("存在实例");
            return
        }

        const instance = new ServiceClass(serviceInter);
        ServiceClass.isInstance = true;
        instance.init();
        ServiceClass[onlyOne] = instance;
        instance[ServiceClass.name] = instance;
        instance.mounted();
        return instance;
    }

    protected  init(){};
    protected mounted(){};
}
